package com.linjiang;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8081);
            while (true){
                Socket socket = serverSocket.accept();
                System.out.println("有客户端连接。。。。。。。");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            handler(socket);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void handler(Socket socket) throws IOException {
        byte[] bytes = new byte[1024];
        int read = socket.getInputStream().read(bytes);
        if (read != -1){
            System.out.println(new String(bytes, 0, read));
        }
    }
}
